package com.jsoft.jsofted.framework.controller;

import com.diboot.core.service.BaseService;
import com.jsoft.jsofted.framework.annotation.JwtToken;
import com.jsoft.jsofted.framework.entity.IdEntity;
import com.jsoft.jsofted.framework.entity.SortEntity;
import com.jsoft.jsofted.framework.entity.TimeEntity;
import com.jsoft.jsofted.framework.service.CocService;
import com.jsoft.jsofted.framework.utils.PageResult;
import com.jsoft.jsofted.framework.utils.ResponseModel;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

public abstract class DataController<E> {

    public abstract CocService<E> cocService();

    public BaseService<E> diService() {
        return (BaseService) cocService();
    }

    @JwtToken
    @RequestMapping("/add")
    public ResponseModel add(@RequestBody E entity) {
        return this._add(entity);
    }

    protected ResponseModel _add(E entity) {
        boolean success = false;

        if (entity instanceof IdEntity) {
            ((IdEntity) entity).setId(null);
        }
        if (entity instanceof TimeEntity) {
            Timestamp time = new Timestamp(System.currentTimeMillis());
            ((TimeEntity) entity).setCreateTime(time);
            ((TimeEntity) entity).setUpdateTime(time);
        }
        if (entity instanceof SortEntity) {
            ((SortEntity) entity).setSortValue(new BigDecimal(System.currentTimeMillis()));
        }

        success = diService().createEntity(entity);
        if (success) {
            return ResponseModel.makeSuccess(null, entity);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/updateList")
    public ResponseModel updateList(@RequestBody List<E> entityList) {
        return this._updateList(entityList);
    }

    protected ResponseModel _updateList(List<E> entityList) {
        boolean success = true;

        for (E entity : entityList) {
            success = success && diService().updateEntity(entity);
        }

        if (success) {
            return ResponseModel.makeSuccess(null, entityList);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/update/{id}")
    public ResponseModel update(@PathVariable String id, @RequestBody E entity) {
        return this._update(id, entity);
    }

    protected ResponseModel _update(String id, E entity) {
        boolean success = false;

        if (entity instanceof IdEntity) {
            ((IdEntity) entity).setId(Long.parseLong(id));
        }
        success = diService().updateEntity(entity);

        if (success) {
            return ResponseModel.makeSuccess(null, entity);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/delete/{id}")
    public ResponseModel delete(
            @PathVariable(required = false) String id,
            @RequestBody(required = false) LinkedList<String> idList
    ) {
        return this._delete(id, idList);
    }

    protected ResponseModel _delete(String id, LinkedList<String> idList) {
        boolean success = false;
        if (idList != null && idList.size() > 0) {
            success = cocService().removeByIds(idList);
        } else if (id != null && id.trim().length() > 0) {
            success = cocService().removeById(id);
        }

        if (success) {
            return ResponseModel.makeSuccess(null, null);
        } else {
            return ResponseModel.makeError(null);
        }
    }

    @JwtToken
    @RequestMapping("/get/{id}")
    public ResponseModel get(@PathVariable String id) {
        return this._get(id);
    }

    protected ResponseModel _get(String id) {
        Object entity = diService().getViewObject(id, cocService().getEntityClass());

        if (entity != null) {
            return ResponseModel.makeSuccess(null, entity);
        } else {
            return ResponseModel.makeError("数据不存在！");
        }
    }

    @JwtToken
    @RequestMapping("/query")
    public ResponseModel query(@RequestBody(required = false) LinkedHashMap queryParams) {
        return this._query(queryParams);
    }

    protected ResponseModel _query(LinkedHashMap queryParams) {
        PageResult pageResult = cocService().queryPageResult(queryParams);

        return ResponseModel.makeSuccess(null, pageResult);
    }


    @JwtToken
    @RequestMapping("/list")
    public ResponseModel list(@RequestBody(required = false) LinkedHashMap queryParams) {
        return this._list(queryParams);
    }

    protected ResponseModel _list(LinkedHashMap queryParams) {
        List list = cocService().queryList(queryParams);

        return ResponseModel.makeSuccess(null, list);
    }

    protected ResponseModel _sort(LinkedHashMap<String, Object> sortParams) {
        String prevRecordId = "" + sortParams.get("prevRecordId");
        String currRecordId = "" + sortParams.get("currRecordId");
        String nextRecordId = "" + sortParams.get("nextRecordId");

        boolean success = cocService().sort(prevRecordId, currRecordId, nextRecordId);
        if (success)
            return ResponseModel.makeSuccess();
        else
            return ResponseModel.makeError();
    }

}
